#include<cstdio>
using namespace std;
int main()
{
	freopen("beibao1.in","r",stdin);
	freopen("beibao1.out","w",stdout);
	int f[1001]={0},w[101]={0},c[101]={0},t,m,i,j;
	scanf("%d %d",&t,&m);
	for(i=1;i<=m;i++)
	scanf("%d%d",&w[i],&c[i]); 
	for(i=1;i<=m;i++)
	for(j=t;j>=w[i];j--)
	if(f[j]<f[j-w[i]]+c[i])
	f[j]=f[j-w[i]]+c[i];
	printf("%d",f[t]);
	fclose(stdin);
	fclose(stdout);
}
